/**   
 *    
 * @file
 * @brief
 * @文件名	: PhotoViewWithLoadingCircle
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-7-16 下午4:31:07 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.widget.ShowBigImage;

import java.io.FileNotFoundException;

import uk.co.senab.photoview.PhotoView;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.common.android.ui.gif.GifImageView;
import com.common.android.utils.ToastUtils;
import com.product.android.ui.imageHolder.CircleProgress;
import com.starappallcommon.R;

/**
 * 带加载进度圆圈的photoview <br>
 * Created 2014-7-16 下午4:31:07
 * 
 * @version 1.2
 * @author chenqian
 * 
 * @see
 */

public class ImageWithLoadingCircle extends RelativeLayout {
	/** 上下文 */
	private Context mContext;
	/** 用于显示普通图片 */
	private PhotoView mPhotoView;
	/** 用于显示GIF图片 */
	private GifImageView mGifView;
	/** 圆圈进度条布局 */
	private RelativeLayout mOutsideCircle;
	/** 圆圈进度条 */
	private CircleProgress mInsideCircle;

	/**
	 * Creates a new instance of ImageWithLoadingCircle. <br>
	 * Created 2014-7-17 下午4:15:24
	 * 
	 * @param context
	 *            Context
	 */
	public ImageWithLoadingCircle(Context context) {
		super(context);
		init(context, null, 0);
	}

	/**
	 * Creates a new instance of PhotoViewWithLoadingCircle. <br>
	 * Created 2014-7-16 下午4:43:27
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 */
	public ImageWithLoadingCircle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs, 0);
	}

	/**
	 * Creates a new instance of PhotoViewWithLoadingCircle. <br>
	 * Created 2014-7-16 下午4:43:37
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyleAttr
	 *            样式
	 */
	public ImageWithLoadingCircle(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs, defStyleAttr);
	}

	/**
	 * 初始化
	 * 
	 * <br>
	 * Created 2014-7-16 下午4:44:31
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            样式
	 * @author chenqian
	 */
	private void init(Context context, AttributeSet attrs, int defStyle) {
		mContext = context;
		LayoutInflater.from(context).inflate(
				R.layout.photoview_loadingcircle_layout, this, true);
		initView();
	}

	/**
	 * 初始化视图控件
	 * 
	 * <br>
	 * Created 2014-7-16 下午5:59:23
	 * 
	 * @author chenqian
	 */
	private void initView() {
		mPhotoView = (PhotoView) findViewById(R.id.photoView);
		mGifView = (GifImageView) findViewById(R.id.gifView);
		mOutsideCircle = (RelativeLayout) findViewById(R.id.outsideCircle);
		mInsideCircle = (CircleProgress) findViewById(R.id.insideCircle);
	}

	/**
	 * 获取静态图片控件
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:15:52
	 * 
	 * @return PhotoView
	 * @author chenqian
	 */
	public PhotoView getPhotoView() {
		return mPhotoView;
	}

	/**
	 * 获取动图控件
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:16:16
	 * 
	 * @return GifImageView
	 * @author chenqian
	 */
	public GifImageView getGifView() {
		return mGifView;
	}

	/**
	 * 获取进度圆圈布局
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:16:34
	 * 
	 * @return RelativeLayout
	 * @author chenqian
	 */
	public RelativeLayout getOutsideCircle() {
		return mOutsideCircle;
	}

	/**
	 * 获取进度圆圈控件
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:17:06
	 * 
	 * @return CircleProgress
	 * @author chenqian
	 */
	public CircleProgress getInsideCircle() {
		return mInsideCircle;
	}

	/**
	 * 显示圆圈进度
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:19:05
	 * 
	 * @author chenqian
	 */
	public void hideCircle() {
		mOutsideCircle.setVisibility(View.GONE);
	}

	/**
	 * 隐藏圆圈进度
	 * 
	 * <br>
	 * Created 2014-7-16 下午6:19:20
	 * 
	 * @author chenqian
	 */
	public void showCircle() {
		mOutsideCircle.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示进度
	 * 
	 * <br>
	 * Created 2014-7-17 上午9:44:25
	 * 
	 * @param progress
	 *            进度
	 * @author chenqian
	 */
	public void showProgress(int progress) {
		mInsideCircle.setMainProgress(progress);
	}

	/**
	 * 显示静态图
	 * 
	 * <br>
	 * Created 2014-7-17 下午7:05:31
	 * 
	 * @param resId
	 *            默认资源图片id
	 * @author chenqian
	 */
	public void showImage(int resId) {
		mPhotoView.setVisibility(View.VISIBLE);
		hideGif();
		if (resId > 0) {
			mPhotoView.setImageResource(resId);
		}
	}

	/**
	 * 显示静态图
	 * 
	 * <br>
	 * Created 2014-7-17 下午5:46:59
	 * 
	 * @param bmp
	 *            Bitmap
	 * @author chenqian
	 */
	public void showImage(Bitmap bmp) {
		mPhotoView.setVisibility(View.VISIBLE);
		hideGif();
		if (null != bmp) {
			mPhotoView.setImageBitmap(bmp);
		}
	}

	/**
	 * 隐藏静态图
	 * 
	 * <br>
	 * Created 2014-7-17 下午2:04:39
	 * 
	 * @author chenqian
	 */
	public void hideImage() {
		mPhotoView.setVisibility(View.GONE);
	}

	/**
	 * 隐藏动态图（gif）
	 * 
	 * <br>
	 * Created 2014-7-17 下午2:06:03
	 * 
	 * @author chenqian
	 */
	public void hideGif() {
		mGifView.setVisibility(View.GONE);
	}

	/**
	 * 显示动态图
	 * 
	 * <br>
	 * Created 2014-7-17 下午3:47:11
	 * 
	 * @param filePath
	 *            图片路径
	 * @param rid
	 *            动态图展示失败时显示的默认图
	 * @author chenqian
	 */
	public void showGif(String filePath, int rid) {
		mGifView.setVisibility(View.VISIBLE);
		hideImage();
		try {
			mGifView.setGifImage(filePath);
			return;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		ToastUtils.display(R.string.show_gif_fail);
		if (rid > 0) {
			mGifView.setImageResource(rid);
		}
	}

	/**
	 * 动态图显示的默认图
	 * 
	 * <br>
	 * Created 2014-7-17 下午3:51:30
	 * 
	 * @param bmp
	 *            动态图展示失败时显示的默认图
	 * @author chenqian
	 */
	public void showGif(Bitmap bmp) {
		if (null == bmp) {
			return;
		}
		mGifView.setVisibility(View.VISIBLE);
		mPhotoView.setVisibility(View.GONE);
		mGifView.setImageBitmap(bmp);
	}

	/**
	 * 是否在加载进度
	 * 
	 * <br>
	 * Created 2014-7-17 下午6:59:09
	 * 
	 * @return 是否在加载进度
	 * @author chenqian
	 */
	public boolean isLoading() {
		return mOutsideCircle.getVisibility() == View.VISIBLE;
	}
}
